package com.tang.thepath;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.tang.thepath.model.RequestModel;
import com.tang.thepath.service.HttpSubscriber;
import com.tang.thepath.view.ImageUploader;

import java.util.ArrayList;
import java.util.List;

public class UploadImageActivity extends BaseActivity {

    private ImageUploader imageUploader;

    private Button btnUpload;

    @Override
    public ImageUploader getImageUploader() {
        return imageUploader;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_image);
        imageUploader = (ImageUploader) findViewById(R.id.img_uploader);
        btnUpload = (Button) findViewById(R.id.btn_upload);

        btnUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(imageUploader.imageCount() == 0){
                    toast("图片数量为空");
                    return;
                }
                showLoading("正在上传图片");
                imageUploader.uploadImages().subscribe(new HttpSubscriber<RequestModel.UploadResponse>() {
                    @Override
                    protected void onSuccess(RequestModel.UploadResponse uploadResponse) {
                        dismisLoading();
                        ArrayList<String> list = (ArrayList<String>)uploadResponse.pathList;
                        Intent intent = new Intent();
                        intent.putStringArrayListExtra("images",list);
                        setResult(RESULT_OK,intent);
                        finish();
                    }
                });
            }
        });
    }
}
